<?php
namespace control;
/*
*现金流类
*/

class cashClass extends adIndexClass
{
    private const page = 'cash';
    private const table = 'cash';
    private const title = '现金流';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            default:
                $tr = [
                    'code'          => [
                        'name'  => "现金流编码",
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'name'  => [
                        'name'  => '现金流名称',
                        'width' => 600,
                    ],
                    'type'         => [
                        'name'  => '现金流类别',
                        'width' => 400,
                    ],
                    'text'          => [
                        'name'  => '备注',
                        'width' => 300,
                    ],
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    "编码" => text(array("name" => "code")),
                    "名称"   => text(array("name" => "name")),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button='';
        //$button =$this->upButton(array("title" => "导入", "fun" => "importData"));
        $button .= "  <a href='" . root . "control/adIndex.html?cla=" . self::page . "&fun=exportData'><span class='spanButton'>导出</span></a>\n";
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "name" => $this->post('name'),
            "code" => $this->post('code'),
        );
        //全等查询数组
        $equal = array(
            'coid' => self::$coid
        );
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'code',
            "sort" => $this->post('list_sort') ?: "asc",
            "array"   => array(),
        ));
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal));
        $page = $this->page(array("table" => self::table, "where" => $where,'cla'=>self::page, "order" => $orderBy['orderBy']));
        static::rSet(self::$stid.self::page. 'sql', $page['fullSql']);
        //获取自定义字段参数
        $installSearch = $this->installSearch(self::page.'-homeSearch');
        $body          = [];
        //表格控件区
        $liButton = array();
        //获取记录
        foreach ($page['array'] as $array) {
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = '';
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /**
     * 导出
     */
    public function exportData()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $tr = [];
            $sql = static::rGet(self::$stid.self::page . 'sql');
            $title = array("现金流编码", "现金流名称", "现金流类别", '备注');
            $result = self::sqlFind($sql);
            if (is_array($result)) {
                foreach ($result as $array) {
                    $tr[] = array($array['code'], $array['name'], $array['type'], $array['text']);
                }
            }
            $text = $this->control['name'] . "导出了" . self::title;
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $tr, self::title . ".xlsx");
        }
        header("Location:" . getenv("HTTP_REFERER"));
        exit(0);
    }

    /**
     * 下载模板
     */
    public function downloadModel()
    {
        $title = ["*现金流编码", "*现金流名称", '现金流类别', '备注'];
        $data[] = ['1', '销售产成品、商品、提供劳务收到的现金', '经营活动产生的现金流量', ''];
        $text = $this->control['name'] . "下载了现金流导入模板";
        $this->logText(["type" => self::page, "text" => $text]);
        $this->excelOut($title, $data, '现金流导入模板.xlsx');
        exit(0);
    }

    /*数据导入分析*/
//    public function importData()
//    {
//             $fomatArray = [];//导入数据格式同一为TYPE_STRING
//            foreach ($this->importFieldInit() as $value){
//                $fomatArray[] = \Vtiful\Kernel\Excel::TYPE_STRING;
//            }
//            $data = $this->importHead(format: $fomatArray);
//            $x = 0;
//            $text = '';
//            foreach ($data as $i => $row) {
//                if ($i > 0) {
//                    $cell = formSubArray($row);
//                    $field = [
//                        'accountRule'=>'enterpriseRule',
//                        'type' => $cell[2],
//                        'code' => $cell[0],
//                        'name' => $cell[1],
//                        'pinyin' => PYClass::encode($cell[1]),
//                        'text' => $cell[3],
//                        'direction'=>'+'
//                    ];
//
//                    $annotation = [
//                        'code' => '现金流编码',
//                        'name' => '现金流名称',
//                    ];
//                    $rules = [
//                        'code' => 'required',
//                        'name' => 'required',
//                    ];
//                    $info = query(self::table, "code = '$cell[0]' and accountRule = 'enterpriseRule' and coid = '" . self::$coid . "'");
//                    // 校验是否符合规则
//                    $vt = new \verify();
//                    $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
//                    if ($result === false) {
//                        $text .= "第【{$i}】行：" . $vt->getErrs()[0] . "<br>";  // 多个规则提示最前面一条
//                    } else {
//                        if (empty($info['id'])) {
//                            $field = array_merge($field, [
//                                'id' => suiji(),
//                            ]);
//                            $warn = static::insert(["table" => 'cash_demo', "field" => $field]);
//                        } else {
//                            $warn = static::update([
//                                "table" => self::table,
//                                "field" => $field,
//                                "where" => "id = '$info[id]'"
//                            ]);
//                        }
//                        if (in_array($warn, ["新增成功", "更新成功"])) {
//                            $x++;
//                            $text .= "第【{$i}】行导入成功<br>";
//                        } else {
//                            $text .= "第【{$i}】行导入失败：编辑失败<br>";
//                        }
//                    }
//                }
//            }
//            $text = $this->control['name'] . '导入了文件：' . $_FILES['file']['name'] . '：' . $text;
//            $this->logText(["type" => self::page, "text" => $text]);
//            $warn = "成功导入" . $x . "条数据，详情请查看日志！";
//            $_SESSION['warn'] = $warn;
//        header("Location:" . getenv("HTTP_REFERER"));
//        exit(0);
//    }


}

